<template>
  <el-card class="box_card">
    <template #header>
      <div class="manage_list_query">
        <el-form
          :inline="true"
          label-position="left"
          :model="articleListQuery"
          class="demo-form-inline"
        >
          <el-form-item label="分类名称：">
            <el-input
              v-model="articleListQuery.typeName"
              clearable
              placeholder="请输入文章分类名称"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="getArticleTypeList">
              查询
            </el-button>
            <el-button type="primary" @click="addArticleType">
              <span class="iconify" data-icon="subway:add-1" data-width="10">
              </span>
              添加类型
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <el-table
      :data="typeList"
      style="width: 100%"
      row-key="id"
      :header-cell-style="{
        'text-align': 'center',
      }"
      :cell-style="{
        'text-align': 'center',
      }"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="typeName" label="文章分类名称" />

      <el-table-column label="操作" width="160" fixed="right">
        <template #default="scope">
          <el-button
            size="small"
            type="primary"
            @click="modifyArticleType(scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="deleteArticle(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <article-type-add
      ref="articleTypeaddRef"
      @getArticleTypeList="getArticleTypeList"
    />
  </el-card>
</template>
<script lang="ts" setup>
// vue 框架插件
import { onMounted, ref, reactive } from 'vue'
// api 接口
import { getArticleTypeListApi, deleteArticleTypeApi } from '@/api/article'
// 组件
import { ElMessage } from 'element-plus'
import ArticleTypeAdd from './components/ArticleTypeAdd.vue'

onMounted(() => {
  getArticleTypeList()
})
const articleTypeaddRef = ref()
// 请求参数
const articleListQuery = reactive({
  typeName: '',
})
// * 文章分类列表
const typeList = ref([])

/**
 * 获取文章分类列表的方法
 */
const getArticleTypeList = async () => {
  var res: any = await getArticleTypeListApi(articleListQuery)
  if (res.status === 0) {
    typeList.value = res.data
  }
}

// ? 显示添加文章分类页面
const addArticleType = () => {
  articleTypeaddRef.value.showDialog()
}

// ? 显示修改文章分类页面
const modifyArticleType = (id: string) => {
  articleTypeaddRef.value.showDialog(id)
}

// ? 删除文章分类的方法
const deleteArticle = async (id: string) => {
  let res: any = await deleteArticleTypeApi(id)
  console.log(res)
  if (res.status === 0) {
    ElMessage.success('删除文章分类成功')
    getArticleTypeList()
  }
}
</script>
<style lang="scss" scoped>
:v-deep(.el-card__header) {
  padding-bottom: 0;
}

.box_card {
  margin: $mainMargin;
}
</style>
